home *** CD-ROM | disk | FTP | other *** search
/ ftp.hitl.washington.edu / ftp.hitl.washington.edu.tar / ftp.hitl.washington.edu / pub / people / tsoper / CT Explorer / CTControlPanel.cs < prev    next >
Text File  |  2005-06-02  |  6KB  |  224 lines

  1. //CTControlPanel.cs
  2. using System;
  3. using System.Windows.Forms;
  4. using System.Drawing;
  5. using MyCustomControls;
  6.  
  7.  
  8. public class CTControlPanel : Panel
  9. {
  10.     /// <summary>
  11.     /// Required designer variable.
  12.     /// </summary>
  13.     //public System.Windows.Forms.Panel    ctControlPanel;
  14.     private System.Windows.Forms.TrackBar[] tbar_SliceIndex;
  15.     private System.Windows.Forms.TextBox[] tbox_SliceIndex;
  16.     private bool isLinkedToScan;
  17.     private bool isLinkedToView;
  18.     private ViewPanel viewPanel;
  19.     private Scan scan;
  20.     private ScrollSlider ss_WindowLevel;
  21.  
  22.     //TAKE THIS OUT
  23.     public Label lbl;
  24.  
  25.     public CTControlPanel()
  26.     {
  27.         ///
  28.         /// Required for Windows.Forms Class Composition Designer support
  29.         ///
  30.         InitializeComponent();
  31.  
  32.         //add a resize callback
  33.         this.Resize += new EventHandler(CTControlPanel_Resize);
  34.     }
  35.     #region Component Designer generated code
  36.     /// <summary>
  37.     /// Required method for Designer support - do not modify
  38.     /// the contents of this method with the code editor.
  39.     /// </summary>
  40.     private void InitializeComponent()
  41.     {
  42.         this.BorderStyle = BorderStyle.Fixed3D;
  43.         tbar_SliceIndex = new TrackBar[3]; //trackbar for each ortho view
  44.         tbox_SliceIndex = new TextBox[3];//and accompanying textbox                                    
  45.  
  46.         //initialize the trackbars and textboxes
  47.         for(int i = 0; i < 3; i++)
  48.         {
  49.             tbar_SliceIndex[i] = new TrackBar();
  50.             tbar_SliceIndex[i].TickStyle = TickStyle.None;
  51.             tbar_SliceIndex[i].Scroll += new EventHandler(
  52.                 Slider_Scrolled);
  53.             this.Controls.Add(tbar_SliceIndex[i]);
  54.             
  55.             tbox_SliceIndex[i] = new TextBox();
  56.             tbox_SliceIndex[i].KeyUp += new KeyEventHandler(
  57.                                             TextBox_Changed);
  58.             this.Controls.Add(tbox_SliceIndex[i]);
  59.         }
  60.  
  61.         //initialize the window level scroll slider
  62.         ss_WindowLevel = new ScrollSlider();
  63.         ss_WindowLevel.Stretch += new EventHandler(Window_Changed);
  64.         ss_WindowLevel.Scroll += new EventHandler(Level_Changed);
  65.         this.Controls.Add(ss_WindowLevel);
  66.     }
  67.  
  68.     //link the control to an actual view
  69.     public void LinkToViewPanel(ViewPanel vp)
  70.     {
  71.         viewPanel = vp;
  72.         isLinkedToView = true;
  73.         for(int i = 0; i < 3; i++)
  74.         {
  75.             tbar_SliceIndex[i].MouseDown += new MouseEventHandler( 
  76.                                                 tbar_MouseDown);
  77.             tbar_SliceIndex[i].MouseUp += new MouseEventHandler( 
  78.                                                 tbar_MouseUp);
  79.         }
  80.         ss_WindowLevel.MouseDown += new MouseEventHandler(
  81.                                             tbar_MouseDown);
  82.         ss_WindowLevel.MouseUp += new MouseEventHandler(
  83.                                             tbar_MouseUp);
  84.     }
  85.  
  86.     //link the control to a scan that has the index data
  87.     //in Scan.SliceIndex
  88.     public void LinkToScan(Scan s)
  89.     {
  90.         scan = s;
  91.         for(int i = 0; i < 3; i++)
  92.         {
  93.             tbar_SliceIndex[i].SetRange(0,scan.DimensionSize[i]-1);
  94.             tbar_SliceIndex[i].Value = scan.SliceIndex[i];
  95.             tbox_SliceIndex[i].Text = scan.SliceIndex[i].ToString();
  96.             ss_WindowLevel.Window = scan.Window/4000f;
  97.             ss_WindowLevel.Level = (scan.Level+2000)/4000f;
  98.         }
  99.         isLinkedToScan = true;
  100.     }
  101.  
  102.     private void Slider_Scrolled(object sender, EventArgs e)
  103.     {
  104.         if(isLinkedToScan) // only process call back if there is
  105.         {                    // a scan that the control links to
  106.             for(int i = 0; i < 3; i++) //figure out which slider
  107.             {                            //made the callback
  108.                 if(sender.Equals(tbar_SliceIndex[i]))
  109.                 {    //found it
  110.                     scan.SliceIndex[i] = tbar_SliceIndex[i].Value;
  111.                     tbox_SliceIndex[i].Text =  
  112.                         tbar_SliceIndex[i].Value.ToString();
  113.                 }
  114.                 Application.DoEvents(); //update text box as scan is scrolled
  115.             }
  116.  
  117.             if(isLinkedToView)
  118.             {
  119.                 viewPanel.Refresh();
  120.             }
  121.             
  122.         }
  123.     }
  124.  
  125.     private void TextBox_Changed(object sender, KeyEventArgs e)
  126.     {
  127.         if(isLinkedToScan) // only process call back if there is
  128.         {                    // a scan that the control links to
  129.             for(int i = 0; i < 3; i++) //figure out which Textbox
  130.             {                            //made the callback
  131.                 if(sender.Equals(tbox_SliceIndex[i]))
  132.                 {    //found it
  133.                     int sliceIndex;
  134.                     
  135.                     //make sure the text is numeric
  136.                     try
  137.                     {
  138.                         sliceIndex = Convert.ToInt32(
  139.                             tbox_SliceIndex[i].Text);
  140.                     }
  141.                     catch
  142.                     {
  143.                         //if not set it back to what it was
  144.                         tbox_SliceIndex[i].Text = 
  145.                                 tbar_SliceIndex[i].Value.ToString();
  146.                         break;
  147.                     }
  148.                     //make sure the value is in bounds
  149.                     if(sliceIndex >= scan.DimensionSize[i])
  150.                     {
  151.                         sliceIndex = scan.DimensionSize[i]-1;
  152.                         tbox_SliceIndex[i].Text = sliceIndex.ToString();
  153.                     }
  154.                     if(sliceIndex <= 0)
  155.                     {
  156.                         sliceIndex = 0;
  157.                         tbox_SliceIndex[i].Text = sliceIndex.ToString();
  158.                     }
  159.  
  160.                     //update slider and scan attributes
  161.                     scan.SliceIndex[i] = sliceIndex; 
  162.                     tbar_SliceIndex[i].Value = sliceIndex;
  163.                 }
  164.             }
  165.  
  166.             if(isLinkedToView)
  167.             {
  168.                 viewPanel.Refresh();
  169.             }
  170.         }
  171.  
  172.     }
  173.  
  174.     //resize the CT Control Panel
  175.     private void CTControlPanel_Resize(object sender, EventArgs e)
  176.     {
  177.         int cx = this.Width;
  178.         int cy = this.Height;
  179.         int tboxWidth = 40;
  180.  
  181.         for(int i = 0; i < 3; i++)
  182.         {
  183.             tbar_SliceIndex[i].SetBounds(0,(int)(cy/4.0*i),
  184.                             cx-tboxWidth,(int)(cy/4.0));
  185.             tbox_SliceIndex[i].SetBounds(cx-tboxWidth,(int)(cy/4.0*i),
  186.                             tboxWidth,(int)(cy/4.0));
  187.         }
  188.         ss_WindowLevel.SetBounds(10,(int)(3*cy/4.0)+10,cx-20,(int)(cy/4.0)-20);
  189.     }
  190.  
  191.     private void tbar_MouseDown(object sender, MouseEventArgs e)
  192.     {
  193.         viewPanel.mainSliceView.DrawQuality = DRAW_QUALITY.LOW;
  194.     }
  195.  
  196.     private void tbar_MouseUp(object sender, MouseEventArgs e)
  197.     {
  198.         viewPanel.mainSliceView.DrawQuality = DRAW_QUALITY.HIGH;
  199.         viewPanel.Refresh();
  200.     }
  201.  
  202.     private void Window_Changed(object sender, EventArgs e)
  203.     {
  204.         if(isLinkedToScan)
  205.         {
  206.             scan.Window = (int)(ss_WindowLevel.Window*4000f);
  207.             //lbl.Text = scan.Window + "," + scan.Level;
  208.             viewPanel.Refresh();
  209.         }
  210.     }
  211.  
  212.     private void Level_Changed(object sender, EventArgs e)
  213.     {
  214.         if(isLinkedToScan)
  215.         {
  216.             scan.Level = (int)(ss_WindowLevel.Level*4000 - 2000);
  217.             //lbl.Text = scan.Window + "," + scan.Level;
  218.             viewPanel.Refresh();
  219.         }
  220.     }
  221.  
  222.     #endregion
  223. }
  224.